//
//  VCOfyouji.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "VCOfyouji.h"
#import <AFNetworking.h>
#import "CellOfyouji.h"
#import "HeaderOfGonglue.h"
#import "ModelOfyouji.h"
#import <DKNightVersion.h>

#import "ModelOfzhuyi.h"
#import "GuideOfzhuyi.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface VCOfyouji ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain)UICollectionView *collection;

@property (nonatomic,retain)UICollectionViewFlowLayout *flowlayout;

@property (nonatomic,retain)NSArray *arrKeys;

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,retain)NSMutableDictionary *dic;



@property (nonatomic,assign)BOOL recomplete;


@end

@implementation VCOfyouji

-(void)dealloc{
    [_strurl release];
    [_dic release];
    [_collection release];
    [_flowlayout release];
    [_arr release];
    [_arrKeys release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self geturl];
    [self creatcollection];
    
     self.collection.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
}

-(void)creatcollection{
    [self createFlowLayout];
    self.collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowlayout];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    self.collection.layer.cornerRadius = 10;
    
    self.collection.layer.masksToBounds = YES;
    
    self.collection.layer.borderWidth = 1;
    
    self.collection.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [self.view addSubview:self.collection];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    self.collection.showsVerticalScrollIndicator = NO;
    
    [self.collection registerClass:[CellOfyouji class] forCellWithReuseIdentifier:@"pool1"];
    
    //注册头部
    
     [self.collection registerClass:[HeaderOfGonglue class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForHeader"];
}

-(void)createFlowLayout{
    self.flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.flowlayout.itemSize = CGSizeMake((WIDTH - 30)/2.0f, 40);
    
    self.flowlayout.minimumInteritemSpacing = 10;
    self.flowlayout.minimumLineSpacing = 10;
    
    self.flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.flowlayout.headerReferenceSize = CGSizeMake(WIDTH, 80);
}

-(void)geturl{
    
    
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:self.strurl];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"阿萨大大的");
        } else {
            
            self.dic = [[NSMutableDictionary alloc]init];
            self.arr = [[NSMutableArray alloc]init];
            for (NSDictionary *dic in responseObject) {
                NSMutableArray *array = [NSMutableArray array];
                
                NSString *key = [dic objectForKey:@"category_type"];
                
                for (NSDictionary *dic2 in [dic objectForKey:@"pages"]) {
                    
                    ModelOfyouji *model =[[ModelOfyouji alloc]init];
                    
                    [model setValuesForKeysWithDictionary:dic2];
                    
                    model.children = [NSArray array];
                    
                    model.children = [dic2 objectForKey:@"children"];
                    
//                    NSLog(@"swipe%@",model.children);
                    
                    [array addObject:model];
                    
                    [model release];
                }
                
                [self.dic setObject:array forKey:key];
            }
            self.arrKeys = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
            
            [self.collection reloadData];
            self.recomplete = YES;
            
//            NSLog(@"%@",self.dic);
            
        }
    }];
    [dataTask resume];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    //key 数量
    NSString *key = [self.arrKeys objectAtIndex:section];
    
    NSArray *arr =[self.dic objectForKey:key];
    
    return arr.count;
 
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CellOfyouji *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    if (self.recomplete == YES) {
        NSString *key = [self.arrKeys objectAtIndex:indexPath.section];
        
        NSArray *arr =[self.dic objectForKey:key];
        
        ModelOfyouji *model = [arr objectAtIndex:indexPath.row];
        
        cell.title.text = model.title;
        
    
    }
    
    cell.layer.cornerRadius = 5;
    
    cell.layer.masksToBounds = YES;
    
    cell.layer.borderWidth = 1;
    
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"死亡爆炸");
   
    NSString *key = [self.arrKeys objectAtIndex:indexPath.section];
    NSArray *arr = [self.dic objectForKey:key];
    ModelOfyouji *model = [arr objectAtIndex:indexPath.row];
    
    GuideOfzhuyi *vc = [[GuideOfzhuyi alloc] init];
    vc.arrpass = model.children;
    
    
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];

   
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
  
    NSString *key = [self.arrKeys objectAtIndex:indexPath.section];

    HeaderOfGonglue *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"poolForHeader" forIndexPath:indexPath];
        
    NSString *str = [NSString stringWithFormat:@"%@",key];
    
    [head creatIconwith:str];
    
    return head;
    }

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.arrKeys.count;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
